package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.product.dao.ProductBrandDao;
import com.wing.product.model.entity.ProductBrand;
import com.wing.product.model.request.ProductBrandForm;
import com.wing.product.model.response.ProductBrandVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 产品品牌-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProductBrandServiceImpl extends ServiceImpl<ProductBrandDao, ProductBrand> implements ProductBrandService {

    @Override
    public int add(ProductBrandForm productBrandForm) {

        //校验

        ProductBrand productBrand =new ProductBrand();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProductBrand.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(productBrandForm, productBrand, copyOptions);
        String code=getCode();
        productBrand.setCode("P"+code);
        productBrand.setNo(Integer.valueOf(code));
        return baseMapper.insert(productBrand);
    }

    private String getCode(){
        String code="";
        Integer maxNo=baseMapper.getMaxNo();
        if (maxNo==null){
            maxNo=1;
        }else{
            maxNo+=1;
        }
        for (int i=0;i<4-maxNo.toString().length();i++){
            code+="0";
        }
        return code+maxNo.toString();
    }

    @Override
    public List<ProductBrandVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProductBrandVO> pageList4app(Page page, Map<String, Object> params) {
        List<ProductBrandVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ProductBrandVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
